package cn.tradewin.advisor.pages.project.init;

import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

import cn.tradewin.advisor.commons.selectmodel.CodeLabelSelectModel;
import cn.tradewin.advisor.commons.util.CodeTableSymbols;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.model.Customer;
import cn.tradewin.advisor.persist.services.CodetableService;
import cn.tradewin.advisor.persist.services.CustomerService;

public class CustomerAdd {
	
	@Property
	private Customer customer;

	@Inject
	private Logger log;
	
	@Property
	private String empNo;
	
	@Property
	private SelectModel countryModel;

	@Inject
	private CustomerService customerService;
	
	@Inject
	private CodetableService codetableService;

	
	void onPrepareForRender() throws Exception {
		List<CodeLabel> categories = codetableService.listCategory(CodeTableSymbols.COUNTRY_TYPE, "zh");
		countryModel = new CodeLabelSelectModel(categories);
	}
	
	@Log
	void onPrepare() throws Exception {
		if (customer == null) {
			customer = new Customer();
		}
	}

	@Log
	Object onSubmit() {
		try {
			customerService.addCustomer(customer);
		} catch (Exception e) {
			log.debug(e.getMessage());
		}
		return CustomerBoard.class;
	}
}
